package com.qiyuan.essay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.qiyuan.common.core.page.TableDataInfo;
import com.qiyuan.common.core.domain.PageQuery;
import com.qiyuan.common.utils.StringUtils;
import com.qiyuan.essay.domain.BlogMusic;
import com.qiyuan.essay.domain.bo.BlogMusicBo;
import com.qiyuan.essay.domain.vo.BlogMusicVo;
import com.qiyuan.essay.mapper.BlogMusicMapper;
import com.qiyuan.essay.service.IBlogMusicService;
import org.springframework.stereotype.Service;
import com.qiyuan.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 音乐Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-22
 */
@Service
public class BlogMusicServiceImpl extends ServicePlusImpl<BlogMusicMapper, BlogMusic, BlogMusicVo> implements IBlogMusicService {

    @Override
    public BlogMusicVo queryById(Integer id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<BlogMusicVo> queryPageList(BlogMusicBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogMusic> lqw = buildQueryWrapper(bo);
        Page<BlogMusicVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<BlogMusicVo> queryList(BlogMusicBo bo) {
        LambdaQueryWrapper<BlogMusic> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<BlogMusic> buildQueryWrapper(BlogMusicBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogMusic> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BlogMusic::getStatus, bo.getStatus());
        lqw.like(StringUtils.isNotBlank(bo.getName()), BlogMusic::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getArtist()), BlogMusic::getArtist, bo.getArtist());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), BlogMusic::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getCover()), BlogMusic::getCover, bo.getCover());
        lqw.eq(StringUtils.isNotBlank(bo.getLrc()), BlogMusic::getLrc, bo.getLrc());
        return lqw;
    }

    @Override
    public Boolean insertByBo(BlogMusicBo bo) {
        BlogMusic add = BeanUtil.toBean(bo, BlogMusic.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(BlogMusicBo bo) {
        BlogMusic update = BeanUtil.toBean(bo, BlogMusic.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BlogMusic entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    /**
     * 修改音乐信息
     * @param music 音乐信息
     * @return
     */
    @Override
    public int updateMusicStatus(BlogMusic music) {
        return baseMapper.updateById(music);
    }
}
